﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_NoSAPFin_NoSAPFin002a : AdminBasePage
{
    decimal? receiptId;
    decimal? boxId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BoxId"] != null)
        {
            boxId = decimal.Parse(Request.QueryString["BoxId"]);
        }
        if (Request.QueryString["ReceiptID"] != null)
        {
            receiptId = decimal.Parse(Request.QueryString["ReceiptID"]);
        }
        
        if (!IsPostBack)
        {
            if (boxId.HasValue && receiptId.HasValue)
            {
                FinReceipt finReceipt = new FinReceipt();
                FinBox finBox = new FinBox();
                if (finBox.LoadByPrimaryKey(boxId.Value))
                {
                    //txtBoxNo.Text = finBox.BoxNo;
                    //txtSupplierBoxNo.Text = finBox.SupplierBoxNo;
                    //drpVoucherDescription.SelectedValue = finBox.DocumentType;
                    //if (finBox.NoSapReceiptCount.HasValue)
                    //{ txtNoSapReceiptCount.Text = finBox.NoSapReceiptCount.Value.ToString(); }
                    //else
                    //{ txtNoSapReceiptCount.Text = "0"; }
                    //drpCompany.SelectedValue = finBox.CompanyName + "-" + finBox.CompanyCode;
                    //drpFunctionDept.SelectedValue = finBox.FunctionDept;
                    //if (finBox.BoxLocation.HasValue)
                    //{ 
                    //    BFThirdUnit bFThirdUnit = new BFThirdUnit();
                    //    if (bFThirdUnit.LoadByPrimaryKey(finBox.BoxLocation.Value))
                    //    {
                    //        drpThirdUnitName.SelectedValue=bFThirdUnit.Name;
                    //        BFThirdUnitCollection bFThirdUnitCollection = new BFThirdUnitCollection();
                    //        bFThirdUnitCollection.Query.Where(bFThirdUnitCollection.Query.Name == drpThirdUnitName.SelectedValue)
                    //            .OrderBy(bFThirdUnitCollection.Query.Name.Ascending);
                    //        bFThirdUnitCollection.Query.Load();
                    //        drpCabin.DataSource = bFThirdUnitCollection;
                    //        drpCabin.DataTextField = BFThirdUnitMetadata.ColumnNames.CartName;
                    //        drpCabin.DataValueField = BFThirdUnitMetadata.ColumnNames.ThirdUnitID;
                    //        drpCabin.DataBind();
                    //        drpCabin.SelectedValue = finBox.BoxLocation.Value.ToString();
                        
                    //    }
                    //}
                    //drpStorageStatus.SelectedValue = finBox.StorageStatus;
                }

                if (finReceipt.LoadByPrimaryKey(receiptId.Value))
                {
                    drpEStorageStatus.SelectedValue = finReceipt.EStorageStatus;
                    txtEStorageLocation.Text = finReceipt.EStorageLocation;
                    if (finReceipt.NeedStoreYear.HasValue)
                    { txtNeedStoreYear.Text = finReceipt.NeedStoreYear.Value.ToString(); }
                    //if (finReceipt.FirstStorageDate.HasValue)
                    //{ txtFirstStorageDate.Value = finReceipt.FirstStorageDate.Value.ToString("yyyy-MM-dd"); }

                    txtComments.Text=finReceipt.Comments;
                    //txtHisIndexStart.Text = finReceipt.HisIndexStart;
                    //txtHisIndexEnd.Text = finReceipt.HisIndexEnd;
                    drpManageUser.Text = finReceipt.ManageUser;
                //    if (!string.IsNullOrEmpty(finReceipt.HisDocumentStartDate))
                //    {
                //        switch (finReceipt.HisDocumentStartDate.Length)
                //        {
                //            case 4:
                //                drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate;
                //                break;
                //            case 6:
                //                drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(0, 4);
                //                drpHisDocumentMonthStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(4, 2);
                //                break;
                //            case 8:
                //                drpHisDocumentYearStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(0, 4);
                //                drpHisDocumentMonthStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(4, 2);
                //                drpHisDocumentDayStart.SelectedValue = finReceipt.HisDocumentStartDate.Substring(6, 2);
                //                break;
                //        }
                //    }

                //    if (!string.IsNullOrEmpty(finReceipt.HisDocumentEndDate))
                //    {
                //        switch (finReceipt.HisDocumentEndDate.Length)
                //        {
                //            case 4:
                //                drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate;
                //                break;
                //            case 6:
                //                drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(0, 4);
                //                drpHisDocumentMonthEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(4, 2);
                //                break;
                //            case 8:
                //                drpHisDocumentYearEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(0, 4);
                //                drpHisDocumentMonthEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(4, 2);
                //                drpHisDocumentDayEnd.SelectedValue = finReceipt.HisDocumentEndDate.Substring(6, 2);
                //                break;
                //        }
                //    }
                }
 
            }
           
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinReceipt finReceipt = new FinReceipt();
        FinBox finBox = new FinBox();
        FinBoxReceipts finBoxReceipts = new FinBoxReceipts();
        string user = LoginUserInfo.CurrentUser.LoginName;
        DateTime today = DateTime.Now;
        string content = string.Empty;

        using (esTransactionScope tr = new esTransactionScope())
        {
            finBox.LoadByPrimaryKey(boxId.Value);
            finReceipt.LoadByPrimaryKey(receiptId.Value);
            //string[] companyInfo = drpCompany.SelectedValue.Split('-');
            //if (finBox.BoxNo != txtBoxNo.Text.Trim())
            //{
            //    content += "凭证盒条码从" + finBox.BoxNo + "改成为" + txtBoxNo.Text.Trim() + "\r\n";
            //    finBox.BoxNo = txtBoxNo.Text.Trim(); 
            //}
            //if (finBox.SupplierBoxNo != txtSupplierBoxNo.Text.Trim())
            //{
            //    content += "外仓条形码从" + finBox.SupplierBoxNo + "改成为" + txtSupplierBoxNo.Text.Trim() + "\r\n";
            //    finBox.SupplierBoxNo = txtSupplierBoxNo.Text.Trim();
            //}
            //if (finBox.DocumentType != drpVoucherDescription.SelectedValue)
            //{
            //    content += "文档描述从" + getCodeTableValue(finBox.DocumentType) + "改成为" + getCodeTableValue(drpVoucherDescription.SelectedValue) + "\r\n";
            //    finReceipt.VoucherDescription = finBox.DocumentType = drpVoucherDescription.SelectedValue;
            //}
            //if (finBox.NoSapReceiptCount != int.Parse(txtNoSapReceiptCount.Text.Trim()))
            //{
            //    content += "凭证数量从" + finBox.NoSapReceiptCount.Value.ToString() + "改成为" + txtNoSapReceiptCount.Text.Trim() + "\r\n";                
            //    finBox.NoSapReceiptCount = int.Parse(txtNoSapReceiptCount.Text.Trim());
            //}
            //if (companyInfo.Length == 2)
            //{
            //    if (finBox.CompanyName != companyInfo[0])
            //    {
            //        content += "公司名称从" + finBox.CompanyName + "改成为" + companyInfo[0] + "\r\n";
            //        finReceipt.CompanyName = finBox.CompanyName = companyInfo[0];
            //    }
            //    if (finBox.CompanyCode != companyInfo[1])
            //    {
            //        content += "公司代码从" + finBox.CompanyCode + "改成为" + companyInfo[1] + "\r\n";
            //        finReceipt.CompanyCode = finBox.CompanyCode = companyInfo[1];
            //    }
            //}
            //if (finBox.FunctionDept != drpFunctionDept.SelectedValue)
            //{
            //    content += "职能部门从" + getCodeTableValue(finBox.FunctionDept) + "改成为" + getCodeTableValue(drpFunctionDept.SelectedValue) + "\r\n";
            //    finReceipt.FunctionDept = finBox.FunctionDept = drpFunctionDept.SelectedValue;
            //}

            
            //if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
            //{ finReceipt.StorageLocation = finBox.BoxLocation = decimal.Parse(drpCabin.SelectedValue); }
            //finReceipt.StorageStatus = finBox.StorageStatus = drpStorageStatus.SelectedValue;
            //finBox.BoxYear = int.Parse(drpHisDocumentYearStart.SelectedValue);
            //finBox.BoxYearEnd = int.Parse(drpHisDocumentYearEnd.SelectedValue);
            //finBox.BoxMonth = int.Parse(drpHisDocumentMonthStart.SelectedValue);
            //finBox.BoxMonthEnd = int.Parse(drpHisDocumentMonthEnd.SelectedValue);

            finBox.UpdatedBy = user;
            finBox.UpdatedDttm = today;
            finBox.VersionNo = finBox.VersionNo + 1;
            finBox.Save();

            finReceipt.EStorageStatus = drpEStorageStatus.SelectedValue;
            finReceipt.EStorageLocation = txtEStorageLocation.Text.Trim();

            if (!string.IsNullOrEmpty(txtNeedStoreYear.Text.Trim()))
            { finReceipt.NeedStoreYear = int.Parse(txtNeedStoreYear.Text.Trim()); }
            //if (!string.IsNullOrEmpty(txtFirstStorageDate.Value))
            //{
            //    finReceipt.FirstStorageDate = DateTime.Parse(txtFirstStorageDate.Value);
            //    finReceipt.StoreStartDate = DateTime.Parse(DateTime.Parse(txtFirstStorageDate.Value).Year.ToString() + "-12-31");
            //}
            finReceipt.Comments = txtComments.Text.Trim();
            finReceipt.ManageUser = drpManageUser.SelectedItem.Text;
            //finReceipt.HisIndexStart = txtHisIndexStart.Text.Trim();
            //finReceipt.HisIndexEnd = txtHisIndexEnd.Text.Trim();
            //finReceipt.HisDocumentStartDate = drpHisDocumentYearStart.SelectedValue + drpHisDocumentMonthStart.SelectedValue + drpHisDocumentDayStart.SelectedValue;
            //finReceipt.HisDocumentEndDate = drpHisDocumentYearEnd.SelectedValue + drpHisDocumentMonthEnd.SelectedValue + drpHisDocumentDayEnd.SelectedValue;
            finReceipt.UpdatedBy = user;
            finReceipt.UpdatedDttm = today;
            finReceipt.VersionNo = finReceipt.VersionNo + 1;
            finReceipt.Save();

            tr.Complete();

            WebHelper.CloseDialog(true);
        }
    }

   

    protected string getCodeTableValue(string code)
    {
        string result = string.Empty;
        TSCodeTableCollection tSCodeTableCollection = new TSCodeTableCollection();
        tSCodeTableCollection.Query.Where(tSCodeTableCollection.Query.Code == code);
        if (tSCodeTableCollection.HasData)
        {
            result = tSCodeTableCollection[0].Value1;
        }
        return result;
    }

}